﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Sale_Model;
using Sale_Common;
using Sale_DAL;
namespace Sale_Operation
{
    public class ProductInfoOp:Component
    {
        public override bool Save(object obj)
        {
            bool blRes = false;
            db = new DataBase();
            ProductInfo SM = obj as ProductInfo; 
            try
            {
                StringBuilder sbSql = new StringBuilder();
                if (SM.PInfo_Id == 0)
                {
                    sbSql.Append("INSERT INTO ProductInfo (PInfo_Code,PInfo_Name,PInfo_PSort_ID,PInfo_Number,PInfo_Pwd,PInfo_CH,PInfo_Price,PInfo_Cost,PInfo_Memo,State,CreateTime,CreateMan) SELECT ");
                    sbSql.Append("PInfo_Code='" + ValueHandler.GetStringValue(SM.PInfo_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("PInfo_Name='" + ValueHandler.GetStringValue(SM.PInfo_Name, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("PInfo_PSort_ID='" + ValueHandler.GetStringValue(SM.PInfo_PSort_ID, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("PInfo_Number='" + ValueHandler.GetStringValue(SM.PInfo_Number, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("PInfo_Pwd='" + ValueHandler.GetStringValue(SM.PInfo_Pwd, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("PInfo_CH='" + ValueHandler.GetStringValue(SM.PInfo_CH, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("PInfo_Price=" + ValueHandler.GetStringValue(SM.PInfo_Price) + ",");
                    sbSql.Append("PInfo_Cost=" + ValueHandler.GetStringValue(SM.PInfo_Cost) + ",");
                    sbSql.Append("PInfo_Memo='" + ValueHandler.GetStringValue(SM.PInfo_Memo, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("State=1,");
                    sbSql.Append("CreateTime=getdate(),");
                    sbSql.Append("CreateMan=" + LinkService.GetCurrentUser().SUser_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    SM.PInfo_Id = ValueHandler.GetIntNumberValue(SqlHelper.ExecuteScalar(db.Transaction, CommandType.Text, "select @@identity "));
                    db.Transaction.Commit();
                    LinkService.InsertLog(ESysLogType.Operation, "新增产品信息[" + ValueHandler.GetStringValue(SM.PInfo_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.产品管理);

                }
                else
                {
                    sbSql.Append("UPDATE ProductInfo SET ");
                    sbSql.Append("PInfo_Code='" + ValueHandler.GetStringValue(SM.PInfo_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("PInfo_Name='" + ValueHandler.GetStringValue(SM.PInfo_Name, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("PInfo_PSort_ID='" + ValueHandler.GetStringValue(SM.PInfo_PSort_ID, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("PInfo_Number='" + ValueHandler.GetStringValue(SM.PInfo_Number, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("PInfo_Pwd='" + ValueHandler.GetStringValue(SM.PInfo_Pwd, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("PInfo_CH='" + ValueHandler.GetStringValue(SM.PInfo_CH, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("PInfo_Price=" + ValueHandler.GetStringValue(SM.PInfo_Price) + ",");
                    sbSql.Append("PInfo_Cost=" + ValueHandler.GetStringValue(SM.PInfo_Cost) + ",");
                    sbSql.Append("PInfo_Memo='" + ValueHandler.GetStringValue(SM.PInfo_Memo, ValueHandler.CharHandlerType.IsRepChar) + "' ");
                    sbSql.Append("WHERE PInfo_Id=" + SM.PInfo_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    db.Transaction.Commit();
                    LinkService.InsertLog(ESysLogType.Operation, "修改产品信息[" + ValueHandler.GetStringValue(SM.PInfo_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.产品管理);
                }
                blRes = true;

            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Error, "新增/修改产品信息出错！[" + ex.ToString() + "]", EMenuList.产品管理);
            }
            db.Close();
            return blRes;
        }

        public override bool HasExists(object obj)
        {
            db = new DataBase(); 
            bool blRes = true;
            try
            {
                ProductInfo SM = obj as ProductInfo;

                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT * FROM ProductInfo WHERE PInfo_Code='" + SM.PInfo_Code + "' AND PInfo_Id<>" + SM.PInfo_Id + " AND State=1 ");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                    blRes = true;
                else
                    blRes = false;

            }
            catch (Exception ex)
            {
            }
            db.Close();
            return blRes;
        }

        public override bool Delete(object obj)
        {
            db = new DataBase();
            bool blRes = false;
            try
            {
                ProductInfo SM = obj as ProductInfo;
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append("UPDATE ProductInfo SET ");
                sbSql.Append("State=0 ");
                sbSql.Append("WHERE PInfo_Id=" + SM.PInfo_Id);
                SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString());
                blRes = true;
                LinkService.InsertLog(ESysLogType.Operation, "删除基础数据[" + ValueHandler.GetStringValue(SM.PInfo_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.产品管理);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "删除基础数据出错！[" + ex.ToString() + "]", EMenuList.产品管理);
            }
            db.Close();

            return blRes;
        }

        public override System.Data.DataTable GetList(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT  PInfo_Id,PInfo_Code,PInfo_Name,PInfo_PSort_ID,PSort_Name , PInfo_Number,PInfo_Pwd,PInfo_CH,PInfo_Price,PInfo_Cost,A.State,A.CreateTime,A.CreateMan,C.SDeta_RealNumber FROM ProductInfo A LEFT JOIN ProductSort B ON (A.PInfo_PSort_ID=B.PSort_ID) ");
                sbSql.Append(" LEFT JOIN StockDetail C ON (C.SDeta_PInfo_Id=A.PInfo_Id)");
                sbSql.Append(" WHERE A.State=1 ");
                if (!string.IsNullOrEmpty(strCondition))
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY A.CreateTime DESC ");
                db.Data = SqlHelper.FillDataSet(db.Connection, sbSql.ToString(), iCurrentPageIndex, ref iRecordCount, LinkService.PageSize);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "产品列表加载失败！[" + ex.ToString() + "]", EMenuList.产品管理);
            }
            db.Close();
            return db.Data.Tables[0];
        }

        public override object GetModel(object obj)
        {
            ProductInfo SM = obj as ProductInfo;
            db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT  PInfo_Id,PInfo_Code,PInfo_Name,PInfo_PSort_ID,PSort_Name , PInfo_Number,PInfo_Pwd,PInfo_CH,PInfo_Price,PInfo_Cost,A.State,A.CreateTime,A.CreateMan FROM ProductInfo A LEFT JOIN ProductSort B ON (A.PInfo_PSort_ID=B.PSort_ID) ");
                sbSql.Append(" WHERE A.State=1 AND PInfo_Id=" + SM.PInfo_Id);
              
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());                   
                    SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
                    SM.PInfo_CH = ValueHandler.GetStringValue(dr["PInfo_CH"]);
                    SM.PInfo_Code = ValueHandler.GetStringValue(dr["PInfo_Code"]);
                    SM.PInfo_Cost = ValueHandler.GetDecNumberValue(dr["PInfo_Cost"]);
                    SM.PInfo_Name = ValueHandler.GetStringValue(dr["PInfo_Name"]);
                    SM.PInfo_Number = ValueHandler.GetStringValue(dr["PInfo_Number"]);
                    SM.PInfo_Price = ValueHandler.GetDecNumberValue(dr["PInfo_Price"]);
                    SM.PInfo_PSort_ID = ValueHandler.GetIntNumberValue(dr["PInfo_PSort_ID"]);
                    SM.PInfo_Pwd = ValueHandler.GetStringValue(dr["PInfo_Pwd"]);
                }
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "产品列表加载失败！[" + ex.ToString() + "]", EMenuList.产品管理);
            }
            db.Close();
            return SM;
        }
        public ProductInfo GetModelByCode(ProductInfo SM)
        {
         
            db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT  PInfo_Id,PInfo_Code,PInfo_Name,PInfo_PSort_ID,PSort_Name , PInfo_Number,PInfo_Pwd,PInfo_CH,PInfo_Price,PInfo_Cost,A.State,A.CreateTime,A.CreateMan FROM ProductInfo A LEFT JOIN ProductSort B ON (A.PInfo_PSort_ID=B.PSort_ID) ");
                sbSql.Append(" WHERE A.State=1 AND PInfo_Code='" + ValueHandler.GetStringValue(SM.PInfo_Code, ValueHandler.CharHandlerType.IsRepChar) + "'");

                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                    SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
                    SM.PInfo_CH = ValueHandler.GetStringValue(dr["PInfo_CH"]);
                    SM.PInfo_Code = ValueHandler.GetStringValue(dr["PInfo_Code"]);
                    SM.PInfo_Cost = ValueHandler.GetDecNumberValue(dr["PInfo_Cost"]);
                    SM.PInfo_Name = ValueHandler.GetStringValue(dr["PInfo_Name"]);
                    SM.PInfo_Number = ValueHandler.GetStringValue(dr["PInfo_Number"]);
                    SM.PInfo_Price = ValueHandler.GetDecNumberValue(dr["PInfo_Price"]);
                    SM.PInfo_PSort_ID = ValueHandler.GetIntNumberValue(dr["PInfo_PSort_ID"]);
                    SM.PInfo_Pwd = ValueHandler.GetStringValue(dr["PInfo_Pwd"]);
                    SM.PInfo_Id = ValueHandler.GetIntNumberValue(dr["PInfo_Id"]);
                }
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "产品列表加载失败！[" + ex.ToString() + "]", EMenuList.产品管理);
            }
            db.Close();
            return SM;
        }
        public ProductInfo GetModelByName(ProductInfo SM)
        {

            db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT  PInfo_Id,PInfo_Code,PInfo_Name,PInfo_PSort_ID,PSort_Name , PInfo_Number,PInfo_Pwd,PInfo_CH,PInfo_Price,PInfo_Cost,A.State,A.CreateTime,A.CreateMan FROM ProductInfo A LEFT JOIN ProductSort B ON (A.PInfo_PSort_ID=B.PSort_ID) ");
                sbSql.Append(" WHERE A.State=1 AND PInfo_Name='" + ValueHandler.GetStringValue(SM.PInfo_Name, ValueHandler.CharHandlerType.IsRepChar) + "'");

                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                    SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
                    SM.PInfo_CH = ValueHandler.GetStringValue(dr["PInfo_CH"]);
                    SM.PInfo_Code = ValueHandler.GetStringValue(dr["PInfo_Code"]);
                    SM.PInfo_Cost = ValueHandler.GetDecNumberValue(dr["PInfo_Cost"]);
                    SM.PInfo_Name = ValueHandler.GetStringValue(dr["PInfo_Name"]);
                    SM.PInfo_Number = ValueHandler.GetStringValue(dr["PInfo_Number"]);
                    SM.PInfo_Price = ValueHandler.GetDecNumberValue(dr["PInfo_Price"]);
                    SM.PInfo_PSort_ID = ValueHandler.GetIntNumberValue(dr["PInfo_PSort_ID"]);
                    SM.PInfo_Pwd = ValueHandler.GetStringValue(dr["PInfo_Pwd"]);
                    SM.PInfo_Id = ValueHandler.GetIntNumberValue(dr["PInfo_Id"]);
                }
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "产品列表加载失败！[" + ex.ToString() + "]", EMenuList.产品管理);
            }
            db.Close();
            return SM;
        }
    }
}
